home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / dugger / mutex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-05  |  2.7 KB  |  71 lines

  1. #include "mutex.h"
  2. const int ERROR_DUPLICATE_NAME = 285;
  3.  
  4. #include <string.h>
  5. #include <stdio.h>
  6.  
  7. mutex::mutex( const char const* name ) {
  8.  
  9.    // -------------------------------------------------------------------
  10.    // the first order of business is to create the name of the semaphore.
  11.    // to do this, we will need 7 bytes (for "\\sem32\\") plus enough for
  12.    // the name plus one more for the null.
  13.    // -------------------------------------------------------------------
  14.  
  15.    sem_name = new char [ 8 + strlen( name ) ];
  16.  
  17.  
  18.    // --------------------------------------------------------------------
  19.    // now that the memory is allocated, copy "\\sem32\\" and the semaphore
  20.    // name into the new string.
  21.    // --------------------------------------------------------------------
  22.  
  23.    strcpy( sem_name, "\\SEM32\\" );       // create first part of string
  24.    strcat( sem_name, name );              // append the user semaphore name
  25.  
  26.    handle = 0;
  27.    rc = DosCreateMutexSem(                // create the semaphore
  28.       sem_name,                           // name of semaphore
  29.       &handle,                            // handle of semaphore
  30.       0,                                  // unused by OS/2
  31.       0 );                                // initial state is owned
  32.  
  33.    if ( rc == ERROR_DUPLICATE_NAME ) {
  34.  
  35.       // ------------------------------------------------------
  36.       // at some point, this semaphore has already been created
  37.       // so we open it for use here
  38.       // ------------------------------------------------------
  39.  
  40.       handle = 0;
  41.       DosOpenMutexSem( sem_name, &handle );
  42.    }
  43.  
  44.    // --------------------------------------------------------------
  45.    // request the semaphore to block other threads from the resource
  46.    // --------------------------------------------------------------
  47.  
  48.    DosRequestMutexSem( handle, SEM_INDEFINITE_WAIT );
  49.  
  50.    // -------------------------------------------------------------
  51.    // by the time the code reaches here, we either waited for total
  52.    // control, or we created the semaphore.
  53.    // -------------------------------------------------------------
  54.  
  55.    return;
  56. }
  57.  
  58. ///////////////////////////////////////////////////////////////////////////
  59. ///////////////////////////////////////////////////////////////////////////
  60.  
  61. mutex::~mutex() {
  62.  
  63.    // ----------------------------------------------------------------
  64.    // the function that created *this has ended, so we can release the
  65.    // mutex semaphore
  66.    // ----------------------------------------------------------------
  67.  
  68.    DosReleaseMutexSem( handle );
  69.    delete [] sem_name;                    // free memory allocated
  70. }
  71.